<template>
  <transition name="model-fade">
    <div class="dialog-wrapper" v-show="visible"  @click.self="handleWrapperClick">
      <transition name="slide-dialog-fade">
        <div v-show="visible"
             class="dialog"
             ref="dialog"
             :style="style">
          <div class="dialog-header">
            <slot name="title">
              <span class="dialog-title">{{title}}</span>
            </slot>
          </div>
          <div class="dialog-body"  :style="{height:bodyHeight+'px'}"><slot></slot></div>
          <div class="dialog-footer" v-if="$slots.footer">
            <slot name="footer"></slot>
          </div>
        </div>
      </transition>
    </div>
  </transition>
</template>

<script>
  const DefaultHeight = document.documentElement.clientHeight-120;
  const DefaultWidth = 500;
  const HeadHeight = 50;
  export default {
    name: 'Dialog',
    data() {
      return {
        bodyHeight:500
      }
    },

    props: {
      height: {
        type: String,
        default: DefaultHeight + 'px',
      },
      width: {
        type: String,
        default: DefaultWidth + 'px',
      },
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      },

      modal: {
        type: Boolean,
        default: true
      },

      modalAppendToBody: {
        type: Boolean,
        default: true
      },

      lockScroll: {
        type: Boolean,
        default: true
      },

      closeOnClickModal: {
        type: Boolean,
        default: true
      },

      closeOnPressEscape: {
        type: Boolean,
        default: true
      },

      showClose: {
        type: Boolean,
        default: true
      },

      customClass: {
        type: String,
        default: ''
      },

      top: {
        type: String,
        default: '120px'
      },
      beforeClose: Function
    },

    watch: {
      visible(val) {
        this.$emit('update:visible', val);
        if (val) {
          this.$emit('open');
          this.$el.addEventListener('scroll', this.updatePopper);
          this.$nextTick(() => {
            this.$refs.dialog.scrollTop = 0;
          });
        } else {
          this.$el.removeEventListener('scroll', this.updatePopper);
          this.$emit('close');
        }
        this.$nextTick(function () {
          this.bodyHeight = this.$refs.dialog.offsetHeight - HeadHeight * 2;
        });
      }
    },

    computed: {
      style() {
        return {
         'width':this.width, 'height':this.height, 'top': this.top
        };
      }
    },

    methods: {
      handleWrapperClick() {
        if (!this.closeOnClickModal) return;
        this.handleClose();
      },
      handleClose() {
        if (typeof this.beforeClose === 'function') {
          this.beforeClose(this.hide);
        } else {
          this.hide();
        }
      },
      hide(cancel) {
        if (cancel !== false) {
          this.$emit('update:visible', false);
          this.$emit('visible-change', false);
        }
      },
      updatePopper() {
        this.broadcast('ElSelectDropdown', 'updatePopper');
        this.broadcast('ElDropdownMenu', 'updatePopper');
      }
    },
  };
</script>
<style lang="scss" scope src = '@/assets/css/components/dialog.scss'></style>
